package com.xxhui.applytransformationdemo.annimation;

import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.BounceInterpolator;
import android.view.animation.Transformation;

/**
 * Created by hui on 2016/7/30.
 */
public class MyAnimation extends Animation {
    private int centerWidth;
    private int centerHeight;
    private Camera camera = new Camera();
    private float roteteY;

    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
        //设置默认时长
        setDuration(2000);
        //设置结束动画后保存状态
        setFillAfter(true);
        // 设置默认插值器
        setInterpolator(new BounceInterpolator());
        centerWidth = width/2;
        centerHeight = height/2;
    }

    // 暴露接口-设置旋转角度
    public void setRoteteY(float roteteY) {
        this.roteteY = roteteY;
    }

    /**
     *
     * @param interpolatedTime 插值器的时间因子，取值0~1
     * @param t 一个Matrix的封装
     */
    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        super.applyTransformation(interpolatedTime, t);
        Matrix matrix = t.getMatrix();
        camera.save();
        // 使用Camera设置旋转的角度
        camera.rotateY(roteteY*interpolatedTime);
        //camera.rotateZ(roteteY*interpolatedTime);
        // 将旋转变换作用到matrix上
        camera.getMatrix(matrix);
        camera.restore();
        //matrix.preTranslate(centerWidth,centerHeight);
        matrix.preTranslate(0,0);
        //matrix.postTranslate(-centerWidth,-centerHeight);
        matrix.postTranslate(0,0);

    }
}
